package com.netty.server.processor.up;

import com.netty.db.dao.entity.TableLamp;
import com.netty.db.service.TableLampService;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class UpPlugin0X01 extends DefaultUpPlugin {

    @Resource
    private TableLampService tableLampService;

    @Override
    public byte getCmd() {
        return 0X01;
    }

    @Override
    protected void handler(byte[] body) {
        ByteBuf buf = Unpooled.wrappedBuffer(body);
        try {
            byte switches = buf.readByte();
            byte brightness = buf.readByte();
            TableLamp tableLamp = tableLampService.getById("000000000001");
            if (tableLamp != null) {
                tableLamp.setSwitches(switches == 0);
                tableLamp.setBrightness(brightness);
                tableLampService.updateById(tableLamp);
            }
        } finally {
            buf.release();
        }
    }
}
